package com.matrix.product.controller;

import com.matrix.api.product.dto.ProductCategoryDTO;
import com.matrix.api.product.dto.ProductDTO;
import com.matrix.api.product.vo.ProductCategoryVO;
import com.matrix.api.product.vo.ProductVO;
import com.matrix.common.domain.PageResult;
import com.matrix.common.domain.Result;
import com.matrix.product.service.ProductService;
import jakarta.validation.Valid;
import lombok.RequiredArgsConstructor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * @author 有点甜
 * @since 2025/7/27
 */
@RestController
@RequiredArgsConstructor
public class ProductController {

    private final ProductService productService;

    @PostMapping("/merchant/product/category")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<ProductCategoryVO> createCategory(@RequestHeader("X-User-ID") Long userId, @Valid @RequestBody ProductCategoryDTO categoryDTO) {
        return Result.success(productService.createCategory(userId, categoryDTO));
    }

    @PutMapping("/merchant/product/category/{categoryId}")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<ProductCategoryVO> updateCategory(@RequestHeader("X-User-ID") Long userId, @PathVariable("categoryId") Long categoryId, @Valid @RequestBody ProductCategoryDTO categoryDTO) {
        return Result.success(productService.updateCategory(userId, categoryId, categoryDTO));
    }

    @GetMapping("/merchant/product/categories")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<List<ProductCategoryVO>> listCategories(@RequestHeader("X-User-ID") Long userId) {
        return Result.success(productService.listCategories(userId));
    }

    @DeleteMapping("/merchant/product/category/{categoryId}")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<Void> deleteCategory(@RequestHeader("X-User-ID") Long userId, @PathVariable("categoryId") Long categoryId) {
        productService.deleteCategory(userId, categoryId);
        return Result.success();
    }

    @PostMapping("/merchant/product")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<ProductVO> createProduct(@RequestHeader("X-User-ID") Long userId, @Valid @RequestBody ProductDTO productDTO) {
        return Result.success(productService.createProduct(userId, productDTO));
    }

    @PutMapping("/merchant/product/{productId}")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<ProductVO> updateProduct(@RequestHeader("X-User-ID") Long userId, @PathVariable("productId") Long productId, @Valid @RequestBody ProductDTO productDTO) {
        return Result.success(productService.updateProduct(userId, productId, productDTO));
    }

    @GetMapping("/merchant/products")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<PageResult<ProductVO>> listProducts(@RequestHeader("X-User-ID") Long userId,
                                                      @RequestParam(value = "pageNum", defaultValue = "1") int pageNum,
                                                      @RequestParam(value = "pageSize", defaultValue = "10") int pageSize,
                                                      @RequestParam(value = "categoryId", required = false) Long categoryId,
                                                      @RequestParam(value = "name", required = false) String name) {
        return Result.success(productService.listProducts(userId, pageNum, pageSize, categoryId, name));
    }

    @GetMapping("/merchant/product/{productId}")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<ProductVO> getProductDetails(@RequestHeader("X-User-ID") Long userId, @PathVariable("productId") Long productId) {
        return Result.success(productService.getProductDetails(userId, productId));
    }

    @DeleteMapping("/merchant/product/{productId}")
    @PreAuthorize("hasAuthority('MERCHANT')")
    public Result<Void> deleteProduct(@RequestHeader("X-User-ID") Long userId, @PathVariable("productId") Long productId) {
        productService.deleteProduct(userId, productId);
        return Result.success();
    }

    @GetMapping("/app/product/menu/{shopId}")
    @PreAuthorize("hasAuthority('USER')")
    public Result<List<ProductCategoryVO>> getShopMenu(@PathVariable("shopId") Long shopId) {
        return Result.success(productService.getShopMenu(shopId));
    }

    @PostMapping("/internal/products/batch")
    public Result<List<ProductVO>> getProductsByIds(@RequestBody List<Long> ids) {
        return Result.success(productService.getProductsByIds(ids));
    }

    @PutMapping("/internal/products/batch-update-ratings")
    public Result<Void> batchUpdateProductRatings(@RequestBody Map<Long, BigDecimal> ratingsMap) {
        productService.batchUpdateProductRatings(ratingsMap);
        return Result.success();
    }

    @PutMapping("/internal/products/batch-update-monthly-sales")
    public Result<Void> batchUpdateMonthlySales(@RequestBody Map<Long, Integer> salesMap) {
        productService.batchUpdateMonthlySales(salesMap);
        return Result.success();
    }
}
